﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvalonDock;
using RiskMan.Config;
using RiskMan.DataClass.Models;
using RiskMan.DataClass.Models.Quik;
using RiskMan.DataClass.Models.RM;

namespace RiskMan.Views.Stop
{
    public partial class StopView : DocumentContent
    {
        public List<PositionModel> GetCount(List<EmitentLimitModel> list)
        {
            List<PositionModel> result = new List<PositionModel>();
            for ( int i = 0; i < list.Count; i++ )
            {
               // if ( list [ i ].CurrentOst != 0 )
                {
                    result.Add( new PositionModel( )
                    {
                        Account = list [ i ].Account,
                        ClientCode = list [ i ].ClientCode,
                        Seccode = list [ i ].Seccode,
                        Count = list [ i ].CurrentOst / mainWindow._dataProvider.GetLots( list [ i ].Seccode )
                    } );
                }
            }
            return result;
        }

        private IEnumerable<PositionModel> GetCount(List<PositionToClientAccountModel> list)
        {
            List<PositionModel> result = new List<PositionModel>();
            for (int i = 0; i < list.Count; i++)
            {
                result.Add(new PositionModel()
                {
                    Account = list[i].Account,
                    ClientCode = list[i].Account,
                    Seccode = list[i].Seccode,
                    Count = list[i].CurrentEmptyPos 
                });
            }
            return result;
        }

        public DealModel GetLastDeal(List<DealModel> list, PositionModel pm)
        {
            DealModel result = new DealModel();
            string op = "";
            if (pm.Count < 0)
                op = cfg.Sell;
            if (pm.Count > 0)
                op = cfg.Buy;

            for (int i = list.Count - 1; i >= 0; i--)
            {
                if(list[i].Operation == op)
                    if (list[i].Seccode == pm.Seccode)
                        return list[i];
            }
            
            return new DealModel();
        }

        public bool SearchingStop(String seccode)
        {
            for (int i = 0; i < mainWindow._dataProvider.StopList.Count; i++)
            {
                if (mainWindow._dataProvider.StopList[i].Seccode == seccode)
                    return true;
            }
            return false;
        }

        public bool IsRun ( PositionModel pm )
        {
            for ( int i = 0; i < mainWindow._dataProvider.StopList.Count; i++ )
            {
                if ( mainWindow._dataProvider.StopList [ i ].Seccode == pm.Seccode &&
                     mainWindow._dataProvider.StopList [ i ].Account == pm.Account &&
                     mainWindow._dataProvider.StopList [ i ].IsStart == true )
                    return mainWindow._dataProvider.StopList [ i ].IsStart;
            }
            return false;
        }

        public List<StopListModel> GetStops(string seccode)
        {
            List<StopListModel> result = new List<StopListModel>();

            for (int i = 0; i < mainWindow._dataProvider.StopList.Count; i++)
            {
                if (mainWindow._dataProvider.StopList[i].Seccode == seccode)
                    result.Add(mainWindow._dataProvider.StopList[i]);
            }

            return result;
        }


    }
}
